#!/usr/bin/perl
use strict;
use warnings;
use Test::More qw/no_plan/;

BEGIN { use FindBin;
        require "$FindBin::Bin/bin/make_test_env.pl"; # needs to be first, sets test app config
        use_ok('Test::WWW::Mechanize::Catalyst', 'WIAB');
    }

END { require FindBin;
      require "$FindBin::Bin/bin/remove_test_env.pl"
  }


my $mech = Test::WWW::Mechanize::Catalyst->new;

# login
$mech->get_ok('http://localhost/user/login', 'got login page');
$mech->submit_form(
    form_name => 'login',
    fields => {    user => 'test',
                   password => 'test'
               }
);
$mech->content_contains('Logout', 'logged in ok');

# get user admin page
$mech->follow_link_ok({text => 'User admin'}, 'got user admin page');

# change passwd
$mech->submit_form(
    form_name => 'change_pass',
    fields => {
        old_password => 'test',
        password => 'test2',
        password_confirm => 'test2',
    }
);
$mech->content_contains('Password successfully changed', 'changed password ok');

$mech->submit_form(
    form_name => 'change_pass',
    fields => {
        old_password => 'test2',
        password => 'test',
        password_confirm => 'test',
    }
);
$mech->content_contains('Password successfully changed', 'changed password ok');


$mech->get_ok('http://localhost/user/admin', 'got admin page ok');

# change pass - old password incorrect "Old password was incorrect, please try again "
# change pass - new pass do not match "New passwords do not match, please try again "


# add user correctly
$mech->submit_form(
    form_name => 'new_user',
    fields => {
        password => 'test',
        user => 'new',
        nu_password => 'newuser',
        nu_password_confirm => 'newuser',
    }
);
$mech->content_contains('New user new added', 'added new user ok');
$mech->get_ok('http://localhost/user/admin', 'got admin page ok 2');

# add user incorrectly - wrong pass "Your password was incorrect, please try again"
# add user incorectly - pre existing user User kd already exists. Please try again
# new user pass don't match - "New user passwords do not match, please try again"


$mech->submit_form(
    form_name => 'del_user',
    fields => {
        user => 'new',
        password => 'test',
    }
);
$mech->content_contains('Are you sure that you want to delete user new', 'confirm delete page ');
$mech->submit_form();
$mech->content_contains('User new deleted successfully');

$mech->get_ok('http://localhost', 'got home page ok');

$mech->follow_link_ok({text => 'Logout'}, 'logging out');
$mech->content_lacks('Logout', 'logged out ok');


